//
//  NSString+Print.m
//  Saas
//
//  Created by 杨荣 on 2020/6/3.
//  Copyright © 2020 bing. All rights reserved.
//

#import "NSString+Print.h"

@implementation NSString (Print)

+ (NSString *)getPrintTextWithTextInfoArray:(NSArray <NSDictionary *>*)textInfoArray font:(NSStringFont)font {
    
    NSInteger byteUnitForChinese = 2;
    NSInteger byteUnitForNotChinese = 1;
    
    if (font == PrinterPrintTextFontBig) {
        byteUnitForChinese = 6;
        byteUnitForNotChinese = 3;
    }else if (font == PrinterPrintTextFontMiddle) {
        byteUnitForChinese = 2;
        byteUnitForNotChinese = 1.5;
    }else {
        byteUnitForChinese = 2;
        byteUnitForNotChinese = 1;
    }
    
    NSString *printText = @"";
    
    for (NSInteger i = 0; i < textInfoArray.count; i++) {
        
        NSDictionary *dic = textInfoArray[i];
        NSString *key = [NSString stringWithFormat:@"%@",dic.allKeys.firstObject];// 文字
        NSString *value = [NSString stringWithFormat:@"%@",dic.allValues.firstObject];// 起始位置
        NSInteger byteOriginX = [value integerValue];
        
        
        NSInteger lastByteOriginX = 0;// 上一个起始位置
        NSInteger lastTextByteWidth = 0;// 上一个文字字节数
        if (i > 0) {
            NSDictionary *lastDic = textInfoArray[i-1];
            NSString *lastKey = [NSString stringWithFormat:@"%@",lastDic.allKeys.firstObject];// 上一个文字
            NSString *lastValue = [NSString stringWithFormat:@"%@",lastDic.allValues.firstObject];// 上一个起始位置
            lastByteOriginX = [lastValue integerValue];
            
            NSDictionary *lastTextCharacterDic = [lastKey characterStatistics];
            NSInteger lastTextCharacterCountForChinese = [[lastTextCharacterDic valueForKey:@"CharacterCountForChinese"] integerValue];// 中文字符个数
            NSInteger lastTextCharacterCountForNotChinese = lastKey.length-lastTextCharacterCountForChinese;// 其它非中文字符个数
            lastTextByteWidth = lastTextCharacterCountForChinese*byteUnitForChinese+lastTextCharacterCountForNotChinese*byteUnitForNotChinese;
        }
        
        NSInteger length = (byteOriginX-lastByteOriginX-lastTextByteWidth)/byteUnitForNotChinese;
        if (length == NSNotFound || length < 0) {
            length = 0;
        }
        NSString *spaceString = [@"" stringByPaddingToLength:length withString:@" " startingAtIndex:0];
        printText = [NSString stringWithFormat:@"%@%@%@",printText,spaceString,key];
        
    }
    
    return printText;
}


+ (NSInteger)getPrintTextByteTotalWithText:(NSString *)text font:(NSStringFont)font {
    NSInteger byteUnitForChinese = 2;
    NSInteger byteUnitForNotChinese = 1;
    
    if (font == PrinterPrintTextFontBig) {
        byteUnitForChinese = 6;
        byteUnitForNotChinese = 3;
    }else if (font == PrinterPrintTextFontMiddle) {
        byteUnitForChinese = 2;
        byteUnitForNotChinese = 1.5;
    }else {
        byteUnitForChinese = 2;
        byteUnitForNotChinese = 1;
    }
    
    NSDictionary *textCharacterDic = [text characterStatistics];
    NSInteger textCharacterCountForChinese = [[textCharacterDic valueForKey:@"CharacterCountForChinese"] integerValue];// 中文字符个数
    NSInteger textCharacterCountForNotChinese = text.length-textCharacterCountForChinese;// 其它非中文字符个数
    NSInteger textByteTotal = textCharacterCountForChinese*byteUnitForChinese+textCharacterCountForNotChinese*byteUnitForNotChinese;
    return textByteTotal;
    
}

- (NSString *)getSubStringWithByteWidthMax:(NSInteger)byteWidthMax font:(NSStringFont)font {
    
    if (byteWidthMax >= 1) {
        byteWidthMax = byteWidthMax-1;// 留一个空格
    }
    
    NSInteger byteUnitForChinese = 2;
    NSInteger byteUnitForNotChinese = 1;
    
    if (font == NSStringFontBig) {
        byteUnitForChinese = 6;
        byteUnitForNotChinese = 3;
    }else if (font == NSStringFontMiddle) {
        byteUnitForChinese = 2;
        byteUnitForNotChinese = 1.5;
        
    }else {
        byteUnitForChinese = 2;
        byteUnitForNotChinese = 1;
    }
    
    NSDictionary *characterDic = [self characterStatistics];
    NSInteger characterCountForChinese = [[characterDic valueForKey:@"CharacterCountForChinese"] integerValue];// 中文字符个数
    NSInteger characterCountForNotChinese = self.length-characterCountForChinese;// 其它非中文字符个数
    NSInteger byteWidth = characterCountForChinese*byteUnitForChinese+characterCountForNotChinese*byteUnitForNotChinese;
    if (byteWidth <=  byteWidthMax) {
        return self;
    }
    
    NSInteger sum = 0;
    for (int i = 0; i<self.length; i++) {
        NSString *temp = [self substringWithRange:NSMakeRange(i,1)];
        const char *u8Temp = [temp UTF8String];
        if (strlen(u8Temp) == 3){
            sum += byteUnitForChinese;
        }else {
            sum += byteUnitForNotChinese;
        }
        
        if (sum >= byteWidthMax) {
            NSString *resultString = [self substringToIndex:i+1];
            return resultString;
        }
    }
    return self;
    
}

- (NSString *)getEffectivePointString {
    
    if (self.length <= 0) {
        return self;
    }
    
    NSString *tempStr = [NSString stringWithFormat:@"%.2f",self.doubleValue];
    NSString *pointString = [tempStr componentsSeparatedByString:@"."].lastObject;
    if ([pointString isEqualToString:@"00"]) {
        NSString *result = [tempStr substringToIndex:tempStr.length-3];
        if (result.floatValue == 0) {
            return @"0";
        }
        return result;
        
    }else {
        
        NSString *lastPointChart = [pointString substringFromIndex:1];
        
        if ([lastPointChart isEqualToString:@"0"]) {
            NSString *result = [tempStr substringToIndex:tempStr.length-1];
            if (result.floatValue == 0) {
                return @"0";
            }
            return result;
        }else {
            if (tempStr.floatValue == 0) {
                return @"0";
            }
            return tempStr;
        }
    }
    
}


- (NSDictionary *)characterStatistics {
    
    NSInteger characterCountForChinese = 0;// 中文字符
    NSInteger characterCountForUpperEnglish = 0;// 大写字母
    NSInteger characterCountForLowerEnglish = 0;// 小写字母
    NSInteger characterCountForNumber = 0;// 数字
    NSInteger characterCountForOther = 0;// 其它字符

    for (int i = 0; i<self.length; i++) {
        char commitChar = [self characterAtIndex:i];
        NSString *temp = [self substringWithRange:NSMakeRange(i,1)];
        const char *u8Temp = [temp UTF8String];
        if (u8Temp == NULL) {
            continue;
        }
        if (strlen(u8Temp) == 3){
//            NSLog(@"字符串中含有中文");
            characterCountForChinese++;
            
        }else if((commitChar>64)&&(commitChar<91)){
//            NSLog(@"字符串中含有大写英文字母");
            characterCountForUpperEnglish++;
            
        }else if((commitChar>96)&&(commitChar<123)){
//            NSLog(@"字符串中含有小写英文字母");
            characterCountForLowerEnglish++;
            
        }else if((commitChar>47)&&(commitChar<58)){
//            NSLog(@"字符串中含有数字");
            characterCountForNumber++;
            
        }else{
//            NSLog(@"字符串中含有其它字符");
            characterCountForOther++;
        }
    }
    
    NSDictionary *dic = @{@"CharacterCountForChinese":@(characterCountForChinese),
                          @"CharacterCountForUpperEnglish":@(characterCountForUpperEnglish),
                          @"CharacterCountForLowerEnglish":@(characterCountForLowerEnglish),
                          @"CharacterCountForNumber":@(characterCountForNumber),
                          @"CharacterCountForOther":@(characterCountForOther),};
    return dic;
}

- (NSInteger)characterCountForChinese {
    NSDictionary *dic = [self characterStatistics];
    return [[dic valueForKey:@"CharacterCountForChinese"] integerValue];
}

- (NSInteger)characterCountForUpperEnglish {
    NSDictionary *dic = [self characterStatistics];
    return [[dic valueForKey:@"CharacterCountForUpperEnglish"] integerValue];
}

- (NSInteger)characterCountForLowerEnglish {
    NSDictionary *dic = [self characterStatistics];
    return [[dic valueForKey:@"CharacterCountForLowerEnglish"] integerValue];
}

- (NSInteger)characterCountForNumber {
    NSDictionary *dic = [self characterStatistics];
    return [[dic valueForKey:@"CharacterCountForNumber"] integerValue];
}

- (NSInteger)characterCountForOther {
    NSDictionary *dic = [self characterStatistics];
    return [[dic valueForKey:@"CharacterCountForOther"] integerValue];
}


+ (NSString *)getPrintPartingLineWithByteWidth:(NSInteger)byteWidth originX:(NSInteger)originX {
    NSString *partingLine = @"-";
    NSInteger partingLineLength = byteWidth-originX;
    for (NSInteger i = 0; i < partingLineLength-1; i++) {
        partingLine = [NSString stringWithFormat:@"%@-",partingLine];
    }
    return partingLine;
}
+ (NSString *)getPrintTextWithLeftText:(NSString *)leftText rightText:(NSString *)rightText byteWidth:(NSInteger)byteWidth {
    
    PrinterPrintTextFont font = PrinterPrintTextFontSmall;
    
    if (leftText.length <=0) {
        leftText = @"";
    }
    if (rightText.length <= 0) {
        rightText = @"";
    }
    
    // 右边文字
    NSDictionary *rightTextCharacterDic = [rightText characterStatistics];
    NSInteger rightTextCharacterCountForChinese = [[rightTextCharacterDic valueForKey:@"CharacterCountForChinese"] integerValue];// 中文字符个数
    NSInteger rightTextCharacterCountForNotChinese = rightText.length-rightTextCharacterCountForChinese;// 其它非中文字符个数
    
    NSInteger byteUnitForChinese = 2;
    NSInteger byteUnitForNotChinese = 1;
    
    if (font == PrinterPrintTextFontBig) {
        byteUnitForChinese = 6;
        byteUnitForNotChinese = 3;
    }else if (font == PrinterPrintTextFontMiddle) {
        byteUnitForChinese = 2;
        byteUnitForNotChinese = 1.5;
    }else {
        byteUnitForChinese = 2;
        byteUnitForNotChinese = 1;
    }
    
    NSInteger rightTextByteOriginX = byteWidth-rightTextCharacterCountForChinese*byteUnitForChinese-rightTextCharacterCountForNotChinese*byteUnitForNotChinese;
    NSString *printText = [NSString getPrintTextWithTextInfoArray:@[@{leftText:@(0)},@{rightText:@(rightTextByteOriginX)}] font:(NSStringFont)font];
    return printText;
}

@end
